package com.zzyweb.rentalcarsys.controller;

import com.zzyweb.rentalcarsys.entity.UserAccount;
import com.zzyweb.rentalcarsys.entity.UserInfo;
import com.zzyweb.rentalcarsys.entity.viewobject.UserVO;
import com.zzyweb.rentalcarsys.service.*;
import com.zzyweb.rentalcarsys.utils.ResponseObject;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhiyi
 * @date 9/29/25
 **/
@RestController
@CrossOrigin
@RequestMapping("/account")
public class LogInController {
    @Autowired
    UserAccountService userAccountService;
    @Autowired
    CaptchaService captchaService;
    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @PostMapping("/logIn")
    public ResponseObject logIn(@Valid @RequestBody UserAccount userAccount, @RequestParam String captchaCode, @RequestParam String captchaId) {
        //第一步 根据用户名密码找到邮箱
        UserAccount user = userAccountService.logIn(userAccount.getUsername(), userAccount.getPassword());
        UserVO userVO = userService.getUserByUserId(user.getId());
        userVO.setRoles(roleService.getRoleOfUser(user.getId()));
        captchaService.verifyImgCaptcha(captchaCode,captchaId);
        //邮箱发送验证码
        captchaService.sendEmailCaptcha(user.getEmail());

        return ResponseObject.ok(userVO);
    }
    @GetMapping("/verify")
    public ResponseObject SecondVerification(@RequestParam String email, @RequestParam String captchaCode) {
        captchaService.verifyEmailCaptcha(email, captchaCode);
        return ResponseObject.ok();
    }
    @PostMapping("/register")
    public ResponseObject register(@Valid @RequestBody UserAccount userAccount) {
        userAccountService.register(userAccount);
        return ResponseObject.ok();
    }
}
